#include "statdata.h"
#include <QtCore/QFile>
#include <QtCore/QStringList>
#include <QtCore/QTextStream>
bool statdataarray::loadfromfile(QString filename,QString splitter)
{
	QFile loadedfile;
	loadedfile.setFileName(filename);
	if(!loadedfile.open(QIODevice::ReadOnly | QIODevice::Text))
		return false;
	this->statdatastorage.resize(0);
	while (!loadedfile.atEnd())
	{
		statdatael tmpel;	
		//parse string
		QString str11=loadedfile.readLine();
		QStringList list1=QString(str11).split(splitter);
		//some overload work
		if(list1.size()==4)
			tmpel.loadedtabletype=1;
		else if(list1.size()==6)
			tmpel.loadedtabletype=2;
		else if(list1.size()==7)
			tmpel.loadedtabletype=3;
		else
			return false;

		//load string to data

		tmpel.player_name=list1[0].trimmed();
		tmpel.player_id=list1[1].toInt();
		tmpel.squad_name=list1[2].trimmed();
		tmpel.team_name=list1[3].trimmed();
		if(tmpel.loadedtabletype>1)
		{
			tmpel.res_num=list1[4].toInt();
			tmpel.shot_num=list1[5].toInt();
		}
		if(tmpel.loadedtabletype>2)
		{
			QStringList list2=QString(list1[6]).split(QString(","));
			for(int j=0;j<MAGICNUMSUCESSIFULLY_PARCED_LIST_LENGTH;j++)
				tmpel.datastring[j]=char(list2[j].toInt());
			//strcpy(tmpel.datastring,list1[6].toAscii().constData());

		}
		else
		{
			for(int j=0;j<MAGICNUMSUCESSIFULLY_PARCED_LIST_LENGTH;j++)
				tmpel.datastring[j]=char(0);

		}
		tmpel.is_data_recieved=false;
		if(tmpel.loadedtabletype>1)
			tmpel.is_data_recieved=true;
		this->statdatastorage.push_back(tmpel);

	}
	//loadedfile.re


	return true;
}
void statdataarray::savetofile(QString filename,QString sep)
{
	QFile loadedfile;
	loadedfile.setFileName(filename);
	loadedfile.open(QIODevice::WriteOnly | QIODevice::Text);
	QTextStream out(&loadedfile);
	//QChar sep=';';
	for(int i=0;i<statdatastorage.size();i++)
	{
		out <<statdatastorage[i].player_name<<sep
			<<statdatastorage[i].player_id<<sep
			<<statdatastorage[i].squad_name<<sep
			<<statdatastorage[i].team_name;
		if(statdatastorage[i].loadedtabletype>1)
			out <<sep<<statdatastorage[i].res_num<<sep
			<<statdatastorage[i].shot_num;
		if(statdatastorage[i].loadedtabletype>1)
		{
			out <<sep;
			for(int j=0;j<MAGICNUMSUCESSIFULLY_PARCED_LIST_LENGTH;j++)
				out <<int(statdatastorage[i].datastring[j])<<",";
		}
		out <<"\n";
	}
	this->calc_additional_elems();
	for(int i=0;i<statdatastorage.size();i++)
	{
		out <<statdatastorage[i].player_id<<sep
		 <<statdatastorage[i].hit_received<<sep
		 <<statdatastorage[i].hit_done<<sep
		 <<"\n";
	}

	//new table
	QFile loadedfile1;
	filename.insert(filename.size()-4,QString("_new_mode"));//.push_front("new_mode_");
	loadedfile1.setFileName(filename);
	loadedfile1.open(QIODevice::WriteOnly | QIODevice::Text);
	QTextStream out1(&loadedfile1);
	//QChar sep=';';
	for(int i=0;i<statdatastorage.size();i++)
	{
		for(int j=0;j<9;j++)
		{
			if(j==LINE_NAME)
				out1 <<statdatastorage[i].player_name<<sep;
			if(j==LINE_ID)
				out1 <<statdatastorage[i].player_id<<sep;
			if(j==LINE_SQUAD)
				out1 <<statdatastorage[i].squad_name<<sep;
			if(j==LINE_TEAM)
				out1 <<statdatastorage[i].team_name<<sep;
			if(j==LINE_RESSURECTED)
				out1 <<statdatastorage[i].res_num<<sep;
			if(j==LINE_SHOTS_DONE)
				out1 <<statdatastorage[i].shot_num<<sep;
			if(j==LINE_HIT_DONE)
				out1 <<statdatastorage[i].hit_done<<sep;
			if(j==LINE_HIT_RECIEVED)
				out1 <<statdatastorage[i].hit_received<<sep;
			if(j==LINE_TIME_RUNNING)
				out1 <<statdatastorage[i].time2go<<sep;
			
		}

		if(statdatastorage[i].loadedtabletype>1)
		{
			out <<sep;
			for(int j=0;j<MAGICNUMSUCESSIFULLY_PARCED_LIST_LENGTH;j++)
				out1 <<int(statdatastorage[i].datastring[j])<<",";
		}
		out1 <<"\n";
		/*out <<statdatastorage[i].player_name<<sep
			<<statdatastorage[i].player_id<<sep
			<<statdatastorage[i].squad_name<<sep
			<<statdatastorage[i].team_name;
		if(statdatastorage[i].loadedtabletype>1)
			out <<sep<<statdatastorage[i].res_num<<sep
			<<statdatastorage[i].shot_num;
		if(statdatastorage[i].loadedtabletype>1)
		{
			out <<sep;
			for(int j=0;j<MAGICNUMSUCESSIFULLY_PARCED_LIST_LENGTH;j++)
				out <<int(statdatastorage[i].datastring[j])<<",";
		}
		out <<"\n";
	}
	for(int i=0;i<statdatastorage.size();i++)
	{
		out <<statdatastorage[i].player_id<<sep
			<<statdatastorage[i].hit_received<<sep
			<<statdatastorage[i].hit_done<<sep
			<<"\n";*/
	}
}
void statdataarray::calc_additional_elems()
{
	//hit received
	for(int i=0;i<this->statdatastorage.size();i++)
	{
		statdatastorage[i].hit_received=0;
		for(int j=0;j<50;j++)
		{
			int ffdf=(int)(this->statdatastorage[i].datastring[j+MAGICNUMSUCESSIFULLY_PARCED_LIST_LENGTH-50]);

			statdatastorage[i].hit_received+=(int)(this->statdatastorage[i].datastring[j+MAGICNUMSUCESSIFULLY_PARCED_LIST_LENGTH-50]);
		}
		//for(int j=0;j<this->statdatastorage.size();j++)
		//{
		//	if(j!=i && i<50)
		//	statdatastorage[i].hit_done+=(int)this->statdatastorage[j].datastring[i+MAGICNUMSUCESSIFULLY_PARCED_LIST_LENGTH-50];
		//	if((int)this->statdatastorage[j].datastring[i+MAGICNUMSUCESSIFULLY_PARCED_LIST_LENGTH-50]<0)
		//	{
		//		int frd=(int)this->statdatastorage[j].datastring[i+MAGICNUMSUCESSIFULLY_PARCED_LIST_LENGTH-50];
		//		int gggs=0;
		//	}
		//	}
		int ggg=0;
	}
	for(int i=0;i<this->statdatastorage.size();i++)
	{
		statdatastorage[i].hit_done=0;
		for(int j=0;j<this->statdatastorage.size();j++)
			if(j!=i && statdatastorage[j].is_data_recieved && statdatastorage[j].player_id<51)
			{
				statdatastorage[i].hit_done+=(int)this->statdatastorage[j].datastring[
					statdatastorage[i].player_id-1+MAGICNUMSUCESSIFULLY_PARCED_LIST_LENGTH-50];
			}

	}
}